Branchwell

Lumen & Gesso / website · lumengesso.com / app / orders / [id] / cancel-button.tsx

1'use client';
2
3import { useState, type FormEvent } from 'react';
4import { useRouter } from 'next/navigation';
5
6// Customer-facing cancel button shown on the order detail page only
7// when the order is in a cancellable state (currently `pending_design`
8// — checked server-side too in /api/orders/[id]/cancel).
9//
10// Two-step UX: a "Cancel this commission" link reveals a confirmation
11// panel with the refund amount + reassurance + Cancel/Keep buttons.
12// Native confirm() would technically work but reads as untrusted on a
13// page like this where the customer just paid real money.
14export function CancelButton({
15 orderId,
16 priceCents,
17 paid = true,
18 refundPending = false,
19}: {
20 orderId: string;
21 priceCents: number;
22 paid?: boolean;
23 refundPending?: boolean;
24}) {
25 const router = useRouter();
26 const [mode, setMode] = useState<'cancel' | 'replace' | null>(refundPending ? 'cancel' : null);
27 const [submitting, setSubmitting] = useState(false);
28 const [error, setError] = useState<string | null>(null);
29
30 const amount = `$${(priceCents / 100).toFixed(2)}`;
31
32 const onCancel = async (e: FormEvent) => {
33 e.preventDefault();
34 setSubmitting(true);
35 setError(null);
36 try {
37 const res = await fetch(`/api/orders/${orderId}/cancel`, { method: 'POST' });
38 if (!res.ok) {
39 const data = await res.json().catch(() => null) as { error?: string } | null;
40 throw new Error(data?.error ?? `Cancel failed (${res.status})`);
41 }
42 // Soft-refresh so the status pill + the cancelled body copy
43 // update. The parent only renders this button while status is
44 // `pending_design`, so after refresh the CancelButton unmounts
45 // entirely. But we still reset submitting + close the panel so
46 // the UI doesn't look hung during the brief refresh window.
47 setSubmitting(false);
48 setMode(null);
49 if (mode === 'replace') router.push(`/builder?edit=${encodeURIComponent(orderId)}&replace=1`);
50 else router.refresh();
51 } catch (err) {
52 setError(err instanceof Error ? err.message : 'Cancel failed.');
53 setSubmitting(false);
54 if (!refundPending) router.refresh();
55 }
56 };
57
58 if (!mode) {
59 return (
60 <div className="cancelorder">
61 <button
62 type="button"
63 className="cancelorder__trigger"
64 onClick={() => setMode('replace')}
65 >
66 Change this order
67 </button>
68 {' · '}
69 <button type="button" className="cancelorder__trigger" onClick={() => setMode('cancel')}
70 >
71 Cancel this commission
72 </button>
73 </div>
74 );
75 }
76
77 return (
78 <form className="cancelorder cancelorder--confirm" onSubmit={onCancel}>
79 <div className="cancelorder__head">{refundPending ? 'Confirm your refund' : mode === 'replace' ? 'Cancel this order and start a replacement?' : 'Cancel this commission?'}</div>
80 <p className="cancelorder__body">
81 {paid && <>We&rsquo;ll request a full refund of <strong>{amount}</strong> to your original payment method. </>}
82 Your uploaded photos stay in Saved photos.
83 {mode === 'replace' && ' We’ll open a new order with your photos and selections. You can change them before checking out again.'}
84 </p>
85 <div className="cancelorder__actions">
86 {!refundPending && <button
87 type="button"
88 className="cancelorder__keep"
89 onClick={() => { setMode(null); setError(null); }}
90 disabled={submitting}
91 >
92 Keep my commission
93 </button>}
94 <button
95 type="submit"
96 className="cancelorder__confirm"
97 disabled={submitting}
98 >
99 {submitting ? 'Working…' : refundPending ? 'Retry refund' : mode === 'replace' ? 'Cancel and start replacement' : paid ? `Yes, cancel and refund ${amount}` : 'Yes, cancel my order'}
100 </button>
101 </div>
102 {error && <p className="cancelorder__err" role="alert">{error}</p>}
103 </form>
104 );
105}